var gulp = require('gulp');
var postcss = require('gulp-postcss');
var px2rem = require('postcss-px2rem');
var connect = require('gulp-connect');
var autoprefixer = require('autoprefixer');
var sourcemaps = require('gulp-sourcemaps');
var plumber = require('gulp-plumber');
var del = require('del');
var less = require('gulp-less');
var cleanCSS = require('gulp-clean-css');

gulp.task('watch', function () {
	gulp.watch(['./src/html/*.html'], ['html']);
	gulp.watch(['./src/less/*.less'], ['css']);
});

var processors = [px2rem({
		remUnit: 75
	}),
	autoprefixer({
		browsers: ['last 2 versions', 'iOS >= 8', 'Android > 4.0']
	})
];

gulp.task('mincss', function () {
	return gulp.src('./src/less/app.less')
		.pipe(sourcemaps.init())
		.pipe(plumber())
		.pipe(less())
		.pipe(postcss(processors))
		.pipe(sourcemaps.write('./'))
		.pipe(cleanCSS())
		.pipe(gulp.dest('./src/css'))
		.pipe(connect.reload());
});

gulp.task('css', function () {
	return gulp.src('./src/less/app.less')
		.pipe(sourcemaps.init())
		.pipe(plumber())
		.pipe(less())
		.pipe(postcss(processors))
		.pipe(sourcemaps.write('./'))
		.pipe(gulp.dest('./src/css'))
		.pipe(connect.reload());
});

//使用connect启动一个Web服务器
gulp.task('connect', function () {
	connect.server({
		root: 'src',
		port: '8000',
		livereload: true,
		host: '0.0.0.0',
	})
});
//html任务
gulp.task('html', function () {
	gulp.src('./src/html/*.html')
		.pipe(connect.reload());
});

gulp.task('clean', function () {
	del.sync(['./src/css/**']);
})

//运行gulp 默认的Task
gulp.task('default', ['clean', 'css', 'connect', 'watch'])
gulp.task('build', ['clean', 'mincss'])